﻿
namespace n_LIDEForm
{
using System;
using System.Globalization;
using System.Windows.Forms;
using n_ImagePanel;
using System.Drawing;
using System.Drawing.Drawing2D;
using c_FormMover;
using n_LCC;

/// <summary>
/// Description of WorkForm.
/// </summary>
public partial class LIDEForm : Form
{
	string FileName;
	string FileNameNoExt;
	bool Changed;
	
	//主窗口
	public LIDEForm()
	{
		InitializeComponent();
		//this.ImeMode = ImeMode.Off;
		
		//fm = new FormMover( this );
		
		try {
			richTextBoxSource.Font = new Font( "consolas", 12 );
			richTextBoxPTree.Font = new Font( "consolas", 12 );
			richTextBoxHEX.Font = new Font( "consolas", 12 );
			richTextBoxWord.Font = new Font( "consolas", 12 );
		}
		catch {
			
		}
		LCC.Init();
	}
	
	//运行
	public void Run()
	{
		this.Visible = true;
		//this.ShowDialog();
		
		this.Text = FileName;
	}
	
	//窗体退出
	void NoteFormFormClosing(object sender, FormClosingEventArgs e)
	{
		if( Changed ) {
			DialogResult dr = MessageBox.Show( "文件已被修改, 是否保存?", "保存提示", MessageBoxButtons.YesNoCancel );
			if( dr == DialogResult.Yes ) {
				n_OS.VIO.SaveTextFile( FileName, richTextBoxSource.Text );
				Changed = false;
			}
			else if( dr == DialogResult.Cancel ) {
				return;
			}
			else {
				Changed = false;
				//... 不保存
			}
		}
		
		e.Cancel = true;
		this.richTextBoxSource.Focus();
		this.Visible = false;
	}
	
	//设置文件名
	void SetFileName( string f )
	{
		FileName = f;
		FileNameNoExt = FileName.Remove( FileName.LastIndexOf( "." ) );
		Text = "已打开文件: " + FileName;
	}
	
	//打开程序文件
	void ButtonOpenFileClick(object sender, EventArgs e)
	{
		//打开文件对话框
		OpenFileDialog OpenFileDlg = new OpenFileDialog();
		OpenFileDlg.Filter = "文本类型文件 | *.*";
		OpenFileDlg.Title = "请选择文件";
		OpenFileDlg.InitialDirectory = G.ccode.FilePath;
		
		DialogResult dlgResult = OpenFileDlg.ShowDialog();
		if(dlgResult == DialogResult.OK) {
			
			string ssFileName = OpenFileDlg.FileName;
			
			if( ssFileName.ToLower().EndsWith( ".lab" ) || ssFileName.ToLower().EndsWith( ".hex" ) ) {
				MessageBox.Show( "请打开txt文件!" );
				return;
			}
			//设置文件名
			SetFileName( ssFileName );
			
			string code = n_OS.VIO.OpenTextFile( FileName );
			richTextBoxSource.Text = code;
			
			Changed = false;
		}
	}
	
	//生成代码
	void Button生成Click(object sender, EventArgs e)
	{
		if( FileName == null ) {
			
			//弹出保存对话框
			SaveFileDialog SaveFileDlg = new SaveFileDialog();
			SaveFileDlg.Filter = "文本类型文件 | *.*";
			SaveFileDlg.Title = "请选择文件";
			SaveFileDlg.InitialDirectory = G.ccode.FilePath;
			
			DialogResult dlgResult = SaveFileDlg.ShowDialog();
			if(dlgResult == DialogResult.OK) {
				string f = SaveFileDlg.FileName;
				if( !f.ToLower().EndsWith( ".txt" ) ) {
					f += ".txt";
				}
				SetFileName( f );
			}
			else {
				return;
			}
		}
		n_OS.VIO.SaveTextFile( FileName, richTextBoxSource.Text );
		Changed = false;
		
		labelMes.Text = "";
		richTextBoxWord.Text = "";
		richTextBoxParse.Text = "";
		richTextBoxASM.Text = "";
		richTextBoxHEX.Text = "";
		richTextBoxCCMes.Text = "";
		
		LCC.Reset();
		string rr = LCC.SetParser( richTextBoxPTree.Text );
		
		string ErrMes = null;
		
		//语法分析
		string[] s = richTextBoxSource.Text.Split( '\n' );
		for( int i = 0; i < s.Length; ++i ) {
			string line = s[i];
			
			//语法分析
			string err = LCC.M_Parse( line );
			if( err != null ) {
				ErrMes += err + "\n";
				continue;
			}
		}
		//汇编器
		if( ErrMes == null ) {
			
			string code = null;
			
			//提取标签
			string err = LCC.M_GetLabel( LCC.ASMResult, ref code );
			if( err != null ) {
				ErrMes += err + "\n";
			}
			
			string[] ss = code.Split( '\n' );
			for( int i = 0; i < ss.Length; ++i ) {
				string line = ss[i];
				line = line.Trim( ' ' );
				if( line == "" ) {
					continue;
				}
				//宏替换
				err = LCC.M_RepLabel( ref line );
				if( err != null ) {
					ErrMes += err + "\n";
					continue;
				}
				//输出到hex文件流
				err = LCC.M_ToHexFile( line );
				if( err != null ) {
					ErrMes += err + "\n";
					continue;
				}
			}
		}
		richTextBoxWord.Text = LCC.WordResult;
		richTextBoxParse.Text = LCC.ParseResult;
		richTextBoxASM.Text = LCC.ASMResult;
		richTextBoxHEX.Text = LCC.HexResult;
		richTextBoxCCMes.Text = LCC.CompMes;
		
		if( ErrMes != null ) {
			labelMes.Text += ErrMes;
		}
		else {
			n_OS.VIO.SaveTextFile( FileNameNoExt + ".hex", LCC.HexResult );
			labelMes.Text = "已生成 hex 机器码文件:\n" + FileNameNoExt + ".hex";
		}
	}
	
	//文本改变
	void RichTextBoxASMTextChanged(object sender, EventArgs e)
	{
		Changed = true;
	}
	
	//保存
	void ButtonSaveClick(object sender, EventArgs e)
	{
		if( FileName == null ) {
			
			//弹出保存对话框
			SaveFileDialog SaveFileDlg = new SaveFileDialog();
			SaveFileDlg.Filter = "文本类型文件 | *.*";
			SaveFileDlg.Title = "请选择文件";
			SaveFileDlg.InitialDirectory = G.ccode.FilePath;
			
			DialogResult dlgResult = SaveFileDlg.ShowDialog();
			if(dlgResult == DialogResult.OK) {
				string f = SaveFileDlg.FileName;
				if( !f.ToLower().EndsWith( ".txt" ) ) {
					f += ".txt";
				}
				SetFileName( f );
			}
			else {
				return;
			}
		}
		n_OS.VIO.SaveTextFile( FileName, richTextBoxSource.Text );
		Changed = false;
	}
}
}



